package com.edais.actions;

import com.edais.service.SystemSettingService;
import com.edais.service.gift.GiftPackageService;
import com.edais.service.imp.GiftPackageExchangeFunctionInvoke;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.JsonUtil;
import com.edais.util.Message;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 礼品
 * Created by haoyx on 2016/11/14.
 */
@Controller
@RequestMapping("/giftPackage")
public class GiftPackageAction {

    private static final Logger logger = Logger.getLogger(GiftPackageAction.class);

    @Autowired
    private GiftPackageService packageService;

    @Autowired
    private SystemSettingService systemSettingService;

    @Autowired
    private GiftPackageExchangeFunctionInvoke packageInvoke;

    /**
     * 	兑换礼包
     * @param request		请求
     * @param response	响应
     */
    @RequestMapping(value = "/giftPackageExchange")
    public void giftPackageExchange(HttpServletRequest request, HttpServletResponse response){
        String result = "";
        try{
            logger.info("#### 打开礼包 ####");

            //判断礼包开关
            String gift_on_off = this.systemSettingService.getSystemSettingValueByName(Consts.GIFT_ON_OFF);
            if(Consts.GIFT_OFF.equals(gift_on_off)){
                logger.info("#### 打开礼包 礼包开关关闭 ####");
                result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
                        Consts.ERROR_CODE, Consts.ERROR_CN, "礼包功能暂未开放");
            }

            Map<String, String> paramsMap = CommonUtil.decryptParamters(request);// 解密请求参数

            String orderId = paramsMap.get("orderId");
            Map<String,Object> params = new HashMap<String, Object>();
            params.putAll(paramsMap);
            Map<String,String> resMap = packageInvoke.apply(orderId,params);

            if(resMap.containsKey("error")){
                result = resMap.get("error");
            }else{
                result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
                        Consts.SUCCESS_CODE, "", Consts.SUCCESS_CN);
            }

        }catch (Exception e){
            logger.error("打开礼包失败：" + e.getMessage());
            result =  CommonUtil.setResultStringCn(new HashMap<String, Object>(),
                    Consts.ERROR_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
        }finally {
            logger.info("#### 打开礼包 ####,result="+result);
            // 处理返回结果
            CommonUtil.responseJson(result, response);
        }
    }

    /**
     * 查询礼包列表
     * @param request       请求
     * @param response      响应
     */
    @RequestMapping(value = "/queryGiftPackageList")
    public void queryGiftPackageList(HttpServletRequest request, HttpServletResponse response){
        String result = null;
        Map<String, Object> jsonMap;
        // 解密请求参数
        Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
        try{
            logger.info("#### 查询礼包列表 ####");

            jsonMap = this.packageService.queryGiftPackageList(paramsMap);
            // 处理返回结果
            result = JsonUtil.getJsonStringFromMap(jsonMap);
        }catch(Exception e){
            logger.error("查询礼包列表失败：" + e.getMessage());
            result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
                    Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
                    Message.CnDescriptionExpression.BUSINESSE_ERROR);
        }finally {
            logger.info("#### 查询礼包列表 ####,result="+result);
            // 处理返回结果
            CommonUtil.responseJson(result, response);
        }
    }

    /**
     * 查询礼包详情
     * @param request       请求
     * @param response      响应
     */
    @RequestMapping(value = "/queryGiftPackageGiftList")
    public void queryGiftPackageGiftList(HttpServletRequest request, HttpServletResponse response){
        String result = null;
        Map<String, Object> jsonMap;
        // 解密请求参数
        Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
        try{
            logger.info("#### 查询礼包礼品列表 ####");

            jsonMap = this.packageService.queryGiftPackageGiftList(paramsMap);
            // 处理返回结果
            result = JsonUtil.getJsonStringFromMap(jsonMap);
        }catch(Exception e){
            logger.error("查询礼包礼品列表失败：" + e.getMessage());
            result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
                    Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
                    Message.CnDescriptionExpression.BUSINESSE_ERROR);
        }finally {
            logger.info("#### 查询礼包礼品列表 ####,result="+result);
            // 处理返回结果
            CommonUtil.responseJson(result, response);
        }
    }
}
